package org.hxd.job.controller;

import org.hxd.job.service.IJobService;
import org.hxd.pojo.Job;
import org.hxd.units.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/job")
public class jobController {
    @Autowired
    IJobService iJobService;
    @RequestMapping("/findjob")
    public String findjob(String name, @RequestParam(defaultValue = "1") int pageIndex, Model model){
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        List<Job> jobs = iJobService.findjob(name,pageModel);
        int rocordcount = iJobService.findjobcount(name);
        pageModel.setRocordCount(rocordcount);
        model.addAttribute("name",name);
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("jobs",jobs);
        return "jsp/job/job";
    }

    @RequestMapping("/findJobById")
    public String findJobById(Model model,int id,int pageIndex,String name){
        Job job = iJobService.findJobById(id);
        model.addAttribute("job",job);
        model.addAttribute("pageIndex",pageIndex);
        model.addAttribute("name",name);
        return "jsp/job/showUpdateJob";
    }

    @RequestMapping("modifyJob")
    @ResponseBody
    public String modifyJob(Job job){
        int rows = iJobService.modifyJob(job);
        if(rows > 0){
            return "OK";
        }else {
            return "FAIL";
        }
    }

    //职位删除
    @RequestMapping("/removeJob")
    @ResponseBody
    public String removeJob(Integer[] ids){
        try {
            int rows = iJobService.removeJob(ids);
            if(rows == ids.length){
                return "OK";
            }else {
                return "FAIL";
            }
        }catch (DataIntegrityViolationException e){
            return "ERROR";
        }
    }

    @RequestMapping("/addJob")
    @ResponseBody
    public Object addJob(Job job,Model model){
        int rows = iJobService.addJob(job);
        if(rows > 0){
            PageModel pageModel = new PageModel();
            int findjobcount = iJobService.findjobcount(null);
            pageModel.setRocordCount(findjobcount);
            return pageModel.getTotalSize();
        }else {
            return "FAIL";
        }
    }
}
